package cn.wolfcode.listener;

import cn.wolfcode.user.entity.User;
import cn.wolfcode.user.service.UserService;
import cn.wolfcode.user.service.impl.UserServiceImpl;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

/**
 * @author 李新雨的项目86176
 * @version 1.0
 * @date 2024-11-08 11:24
 */

//实现接口,指定的接口，服务器创建就监听有没有对象
@WebListener
public class UserListener implements ServletContextListener {
    private UserService userService=new UserServiceImpl();

    //// TODO: 2024/11/8  利用监听器完成数据库的账号初始化，即为管理员身份
    @Override
    public void contextInitialized(ServletContextEvent servletContextEvent) {
        try {
            //服务器开启，监听器找数据库中是否存在该账户
            User lxy6 = userService.login("lxy6", "12345");
            System.out.println("------------------------");
        } catch (Exception e) {
            //没有创建对象，插入到数据库
            User user=new User();
            user.setUsername("lxy6");
            user.setPassword("12345");
            userService.addNew(user);
        }
    }

    /**
     *
     * @param servletContextEvent
     */
    @Override
    public void contextDestroyed(ServletContextEvent servletContextEvent) {

    }
}
